﻿using FPSO.HMI.Utils;
using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Models.ViewModel.Controls;
using Opc.Ua;
using Org.BouncyCastle.Asn1.X509;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml.Linq;

namespace FPSO.HMI.Views
{
    /// <summary>
    /// MonitorView.xaml 的交互逻辑，检验系统间通断
    /// </summary>
    public partial class MonitorView : UserControl
    {
		/// <summary>
		/// 系统通断状态刷新计时器
		/// </summary>
		private DispatcherTimer taskTimer;
		private bool controlmode = false;
		public MonitorView()
        {
            InitializeComponent();

			InitConnectionStatus();
			taskTimer = new DispatcherTimer();
			taskTimer.Interval = TimeSpan.FromSeconds(600);
			taskTimer.Tick += TaskTimer_Tick;
			taskTimer.Start();
		}

		private void TaskTimer_Tick(object sender, EventArgs e)
		{
			updateConnectionStatus();
		}

		private void InitConnectionStatus()
		{
			Path_WKS9102A.StrokeThickness = 2.0;
			Path_WKS9102A.Stroke = new SolidColorBrush(Colors.LightGreen);

			bool status = updateTankConnectionStatus();
			if (!status)
				CommUtil.AddSystemLog(0, 3, "数据异常");

			status = updateEquipConnectionStatus();
			if (!status)
			{
				CommUtil.AddSystemLog(0, 4, "数据异常");
				CommUtil.AddSystemLog(0, 5, "数据异常");
			}
		}

		public bool updateTankConnectionStatus()
		{
			double value_check = 0;
			Boolean checkres = true;
			List<OPCTag> tags = TankConstant.tagList_tankStatus;
			if (tags != null)
			{
				foreach (OPCTag tag in tags)
				{
					if (tag.OpcValue == null)
					{
						checkres = false;
						break;
					}
					value_check += Convert.ToDouble(tag.OpcValue.Value);
				}
				if (value_check == 0)
					checkres = false;
			}
			else
			{
				checkres = false;
			}
			if (checkres == false)
			{
				Path_WKS9102A.Stroke = new SolidColorBrush(Colors.Red);
				Path_WKS9102A.StrokeThickness = 5.0;
			}
			else
			{
				Path_WKS9102A.Stroke = new SolidColorBrush(Colors.LightGreen);
				Path_WKS9102A.StrokeThickness = 2.0;
			}
			return checkres;
		}

		public bool updateEquipConnectionStatus()
		{
			double value_check = 0;
			Boolean checkres = true;

			List<OPCTag> tags = TankConstant.tagList_valveStatus;
			if (tags != null)
			{
				foreach (OPCTag tag in tags)
				{
					if (tag.OpcValue == null)
					{
						checkres = false;
						break;
					}
					value_check += Convert.ToDouble(tag.OpcValue.Value);
				}
				if (value_check == 0)
					checkres = false;
			}
			else
			{
				checkres = false;
			}
			if (checkres == false)
			{
				Path_RCU7802A.Stroke = new SolidColorBrush(Colors.Red);
				Path_RCU7802A.StrokeThickness = 5.0;
				Path_RCU7802B.Stroke = new SolidColorBrush(Colors.Red);
				Path_RCU7802B.StrokeThickness = 5.0;
			}
			else
			{
				if (controlmode)
				{
					Path_RCU7802A.Stroke = new SolidColorBrush(Colors.LightBlue);
					Path_RCU7802B.Stroke = new SolidColorBrush(Colors.LightBlue);
				}
				else
				{
					Path_RCU7802A.Stroke = new SolidColorBrush(Colors.LightGreen);
					Path_RCU7802B.Stroke = new SolidColorBrush(Colors.LightGreen);
				}
				Path_RCU7802A.StrokeThickness = 2.0;
				Path_RCU7802B.StrokeThickness = 2.0;
			}
			return checkres;
		}

		private void updateConnectionStatus()
		{
			bool status = updateTankConnectionStatus();
			if (!status)
				CommUtil.AddSystemLog(0, 3, "数据异常");

			status = updateEquipConnectionStatus();
			if (!status)
			{
				CommUtil.AddSystemLog(0, 4, "数据异常");
				CommUtil.AddSystemLog(0, 5, "数据异常");
			}
		}

		private void ICSSPermit_Click(object sender, RoutedEventArgs e)
		{
			controlmode = !controlmode;
			string tag_start = "ControlMode_V";
			string status_tag0 = string.Format("{0}{1}", SysConfig.OPC_ADD_PRE, tag_start);
			try
			{
				App.UaClient.WriteNode<Int16>(status_tag0, Convert.ToInt16(controlmode));
                Thread.Sleep(2000);
                DataValue data = App.UaClient.ReadNode(status_tag0);
				controlmode = Convert.ToBoolean(data.Value);
				if (controlmode)
					Permit_btn.Background = new SolidColorBrush(Colors.Green);
				else
					Permit_btn.Background = new SolidColorBrush(Colors.Transparent);
			}
			catch (Exception ex) 
			{
				CommUtil.AddSystemLog(0, 1, ex.Message);
			}
		}	
	}
}
